/************************************************************************
 Omnitrix - PHP Development Framework
 Copyright (C) 2012-2018 amonest(eastson@outlook.com)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
************************************************************************/

var JSTEXTS = {};

String.prototype.trim = function() {   
	return this.replace(/(^\s*)|(\s*$)/g, '');   
}

var AdminCP = {
    init: function() {
    	this.navigationMenu();
    	this.setupTableList();
    	$('ul.tabs a').tabs();
    	
    	$('a[data-confirm]').click(function() {
    		$(this).closest('.dropdown-menu').dropdown('hide');
    		var msg = JSTEXTS[$(this).attr('data-confirm')];
    		if (confirm(msg)) location.href = $(this).attr('href');
    		return false;
    	});
    },
    
    setupTableList: function() {
    	$('table.table-list').each(function() {
    		$('th input[type=checkbox]').change(function() {
    			var checked = this.checked;
    			$(this).closest('table').find('td input[type=checkbox]').each(function() {
    				this.checked = checked;
    			});
    		});
    	});
    	
    	$('.bulk-actions').each(function() {
    		var parent = this, box = $(this).attr('target-box'), ids = [];
    		$(this).find('input[type=button]').click(function() {
    			if ($(parent).find("select option:selected").val().length == 0) {
    				alert(JSTEXTS.no_bulk_action);
    				return;
    			}
    			if ($(box).find('td input[type=checkbox]:checked').length == 0) {
    				alert(JSTEXTS.no_checked_rows);
    				return;
    			}
    			$(box).find('td input[type=checkbox]:checked').each(function() {
    				ids.push($(this).val());
    			});
    			var url = $(parent).find("select option:selected").attr('url');
    			location.href = $.param.querystring(url, {ids: ids.join(',')});
    		});
    	});
    	
    	$('.quick-search').each(function() {
    		var parent = this;
    		$(this).find('input[type=button]').click(function() {
    			var s = $(parent).find('input[type=text]').val().trim();
    			location.href = $.param.querystring(location.href, {s: s});
    		});
    		$(this).find('input[type=text]').keydown(function(event) {
    			if (event.keyCode == 13) {
    				event.returnValue = false; 
    		        event.cancel = true;
    				$(parent).find('input[type=button]').click();
    			}
    		});
    	});
    },
    
    navigationMenu: function() {
    	$('#navmenu > ul').superfish({
    		hoverClass	 : 'sfHover',
    		pathClass	 : 'overideThisToUse',
    		delay		 : 0,
    		animation	 : {height: 'show'},
    		speed		 : 'normal',
    		autoArrows   : false,
    		dropShadows  : false,
    		disableHI	 : false,
    		onInit		 : function(){},
    		onBeforeShow : function(){},
    		onShow		 : function(){},
    		onHide		 : function(){}
    	});

    	$('#navmenu > ul').css('display', 'block');
    	
    	var route = this.getURLVar('route');
    	if (!route) {
    		$('#navmenu > ul:first > li:first').addClass('selected');
    	} else {
    		var part = route.split('/');
    		var url = part[0];

    		if (part[1]) {
    			url += '/' + part[1];
    		}

    		$('a[href*=\'' + url + '\']').parents('li').addClass('selected');
    	}
    },
    
    getURLVar: function(urlVarName) {
		var urlHalves = String(document.location).toLowerCase().split('?');
		var urlVarValue = '';
		
		if (urlHalves[1]) {
			var urlVars = urlHalves[1].split('&');
	
			for (var i = 0; i <= (urlVars.length); i++) {
				if (urlVars[i]) {
					var urlVarPair = urlVars[i].split('=');
				
					if (urlVarPair[0] && urlVarPair[0] == urlVarName.toLowerCase()) {
						urlVarValue = urlVarPair[1];
					}
				}
			}
		}
	
		return urlVarValue;
	},

    confirmPost: function(element, message) {
		if (element && confirm(message)) {
			form = document.createElement("form");
			form.setAttribute("method", "post");
			form.setAttribute("action", element.href);
			form.setAttribute("style", "display: none;");
			document.getElementsByTagName("body")[0].appendChild(form);
			form.submit();
		}
		return false;
    },
    
    manualSubmit: function(url, attribs) {
    	var form = $('<form/>', {action: url, method: 'post'});
    	$.each(attribs, function(key, value) {
    		if (value instanceof Array) {
    			$(form).append($('<input/>', {type: 'hidden', name: key, value: value.join(',')}));
    		} else {
    			$(form).append($('<input/>', {type: 'hidden', name: key, value: value}));
    		}
    	});
    	form.submit();
    },
    
    generator: function(name, url) {
    	$.get(url, function(result) {
    		$('input[name=' + name + ']').val(result.content);
    	}, 'json');
    }
};

$(document).ready(function() {
	AdminCP.init();
});